# -*- coding: iso-8859-2 -*-

import re
import math
import email.utils
import os

from authorshipanalyzer.file import ResourceReader
from authorshipanalyzer.slot import Filler
from authorshipanalyzer.utils import getKey
from authorshipanalyzer.analyze import VSMAnalyzer


class VSMFiller(Filler):
    '''
    Class which set category to the message
    @param vsmAnalyzer: vector space model analyzer
    '''

    def initVSMAnalyzer(self, keyWordsDirectory):
        '''
        @param: directory with key words of each category;
        key words for each category are stored in a single 
        file in keyWordsDirectory with txt extension. File
        consists of words stored in a word per line schema.
        '''

        categories = {}
        reader = ResourceReader()
        
        categoryNames = reader.getMechanizmFilesDict(keyWordsDirectory)
        for category in categoryNames.keys():
            categoryWords = reader.readWordFreq(categoryNames[category])
            categories[category] = categoryWords
        
        self.vsmAnalyzer = VSMAnalyzer(categories)
        
    def fill(self, slot):
        '''
        Recognize and set category of the message
        '''
        messageWords = slot.wordFrequency
#        print "!!!!!!!!!!!!!!!"  + str(len(messageWords))
        category = self.vsmAnalyzer.categorize(messageWords)
        self.setCategory(slot, category)
        
    def setCategory(self, slot, category):
        '''
        Method setting category. Should be implemented
        in descendor class.
        @param: slot
        '''

         